//
//  GuideViewController.m
//  God_is_a_Girl
//
//  Created by 李继鹏 on 15/9/23.
//  Copyright (c) 2015年 呸. All rights reserved.
//

#import "GuideViewController.h"
#import <AFNetworking.h>
#import "LJP_GuideCollectionViewCell.h"
#import "LJP_SecondViewController.h"

@interface GuideViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) NSArray *plistArray;
@property (nonatomic, strong) NSArray *picArray;
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation GuideViewController
- (void)collectionViewSubview
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(15 , 40 , 15 , 40);
    flowLayout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 40) / 3, ([UIScreen mainScreen].bounds.size.width - 40) / 3 + 30);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 113) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[LJP_GuideCollectionViewCell class] forCellWithReuseIdentifier:@"qwer"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.plistArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LJP_GuideCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"qwer" forIndexPath:indexPath];
    cell.pic.image = [UIImage imageNamed:self.picArray[indexPath.item]];
    cell.title.text = self.plistArray[indexPath.item][@"tname"];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LJP_SecondViewController *second = [[LJP_SecondViewController alloc] init];
    second.tname = self.plistArray[indexPath.item][@"tname"];
    [self.navigationController pushViewController:second animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
//    self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBar.translucent = NO;

}
- (void)dataHandle
{
    self.picArray = @[@"ListCity",
                      @"ListTrip",
                      @"ListRoad",
                      @"ListWorld",
                      @"ListPlayer",
                      @"ListFind"];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/tagsWithoutCover?categoryId=22&contentType=album&device=android" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.plistArray = [responseObject objectForKey:@"list"];
        [self.collectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);

    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"行听";
    [self dataHandle];
    [self collectionViewSubview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
